package com.bj58.mis.apm.server.core.util;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author: zhangfx
 * @create: 2018-07-20 13:46
 **/
@Component
@ConfigurationProperties(prefix = "apm.wos")
public class WOSClient implements InitializingBean {
    private RestTemplate template = new RestTemplate();
    private String wosTokenHost;
    private String wosFileHost;
    private String appId;
    private String bucket;
    private String secrectId;
    private int ttl = 180*24;
    private int insertOnly = 0;
    private String authKey;

    public void setTemplate(RestTemplate template) {
        this.template = template;
    }

    public void setWosTokenHost(String wosTokenHost) {
        this.wosTokenHost = wosTokenHost;
    }

    public void setWosFileHost(String wosFileHost) {
        this.wosFileHost = wosFileHost;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public void setSecrectId(String secrectId) {
        this.secrectId = secrectId;
    }

    public void setTtl(int ttl) {
        this.ttl = ttl;
    }

    private String getToken(String fileName){
        String url = wosTokenHost + "/get_token?bucket="+bucket+"&filename="+fileName;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Authorization", "Basic "+authKey);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null,requestHeaders);
        ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class);
        String json = response.getBody();
        return (String)JsonUtil.fromJson(json).get("data");
    }
    public void uploadFile(String fileName, String context){
        try {
            String url = wosFileHost + "/" + appId + "/" + bucket + "/" + fileName;
            String token = getToken(fileName);
            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            body.add("filecontent", context.getBytes("UTF-8"));
            body.add("op", "upload");
            body.add("ttl ", ttl);
            body.add("insertOnly", insertOnly);

            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.setContentType(MediaType.parseMediaType("multipart/form-data"));
            requestHeaders.add("Authorization", token);

            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, requestHeaders);
            template.exchange(url, HttpMethod.POST, httpEntity, String.class);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
    public String downloadFile(String fileName){
        String url = wosFileHost + "/" + appId + "/" + bucket + "/" + fileName;
        try {
            ResponseEntity<byte[]> response = template.exchange(url, HttpMethod.GET, new HttpEntity<byte[]>(new HttpHeaders()), byte[].class);
            return new String(response.getBody(), "UTF-8");
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        authKey = new sun.misc.BASE64Encoder().encode((appId + ":" + secrectId).getBytes());
    }
}
